package com.sumer.psychology.photo.application.query;

import com.mongodb.BasicDBObject;
import com.mongodb.DBCursor;
import com.mongodb.DBObject;
import com.sumer.psychology.common.persistence.mongodb.AbstractMongoRepository;

/**
 * 相册查询
 * <p/>
 * User: zhenguang.zhu
 * Date: 2014/10/30
 * Time: 9:44
 */
public class AlbumQueryApplicationService extends AbstractMongoRepository {
    private static final String DB_NAME = "sumer_album";

    /**
     * 按照作者查询相册
     *
     * @param ownerId      owner
     * @param countPerPage count per page
     * @param offset       off set
     * @return json
     */
    public String findByAuthor(String ownerId, int countPerPage, int offset) {
        DBObject queryCondition = new BasicDBObject("owner.identity", ownerId);

        DBCursor results = db().getCollection(dbName())
                .find(queryCondition)
                .skip(offset)
                .limit(countPerPage);

        return serializeWithTotalCount(queryCondition, results);
    }

    @Override
    protected String dbName() {
        return DB_NAME;
    }
}
